-
-
Notifications
You must be signed in to change notification settings - Fork 198
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add From<[Elem; N]>
for Packed Array and Optimize From<Vec<Elem>>
#827
Conversation
API docs are being generated and will be shortly available at: https://godot-rust.github.io/docs/gdext/pr-827 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot!
Not yet very sure about the safety invariant, added some comments. Do we really gain performance with it?
} | ||
ret.resize(len); | ||
let ptr = vec.as_ptr(); | ||
// SAFETY: Data is moved and the original vector is forcibly set to empty. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The // SAFETY
assertion is not just a regular comment that explains what happens, but it needs to reason why and how the safety assumptions (specified in the function docs) are upheld.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would also be good to have an empty line before it.
ac68f66
to
eeb06e2
Compare
let (len, mut ret) = match vec.len() { | ||
0 => return Self::new(), | ||
len => (len, Self::default_with_size(len)), | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is unnecessarily hard to understand, could you just write a
if vec.is_empty() {
return Self::new();
}
instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So something like this?
if vec.is_empty() {
return Self::new();
}
let len = vec.len();
let mut ret = Self::default_with_size(len);
I don't know, but it doesn't save enough space to justify default_with_size
and stand out with above From
s.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, maybe I was too optimistic with the code reuse here... Otherwise it's OK to not have default_with_size
. More important than a bit of duplication or fewer lines of code is readability (which is problematic with the match
version).
It's a bit annoying that Godot doesn't have a "resize without default-construct" (à la But I don't see a way around default-constructing each element and then dropping it. "Dropping" is probably simplest done via The alternative is appending elements to an empty collection (e.g. involving |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some more comments, thanks for the update!
Is there a concrete use case for supporting array conversions? People are always quick to add From
with arrays, but in reality very few people use actual arrays, it's almost always Vec
due to the dynamic nature. The syntax you mentioned is already possible with slices (although it can be tiny bit more expensive due to copy):
PackedInt32Array::from(&[1, 2])
Not against it, just wondering 🙂
b9c97a4
to
3d92626
Compare
3d92626
to
aecf2b4
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add an integration test for From<Vec>
? Maybe with a packed array type that isn't yet used, e.g. Color
...
array | ||
} | ||
|
||
/// Moves elements from slice. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// Moves elements from slice. | |
/// Moves elements from slice. |
Comments should express more than the function name, or be omitted.
Here, you could mention something like
/// Moves elements from slice. | |
/// Drops all elements in self and replaces them with ones in provided pointer range. |
aecf2b4
to
041e4d7
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot for the continuous improvements!
When making
From
array impl, i realized it andVec
are very similiar in semantics.From
array is useful to create smallPackedArray
quickly.No
From<Box<[Elem]>>
since boxed slice can be converted intoVec
as intermediate.